package com.ai.bitcoin.schedule.task;

import com.ai.bitcoin.redis.service.BccRedisService;
import com.ai.bitcoin.util.DateUtil;
import com.ai.bitcoin.vo.HtmlReportGenerateVO;
import com.ai.bitcoin.weixin.push.WxPushHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @description: ${description}
 * @author: gulj
 * @create: 2018-06-15 00:14
 **/
@Slf4j
@Component
public class HtmlReportPushTask {


    @Autowired
    private WxPushHelper wxPushHelper;


    @Autowired
    private HtmlReportGenerateAllTask htmlReportGenerateAllTask;

    @Autowired
    private BccRedisService bccRedisService;

    /**
     * 推送报告
     */
    @Scheduled(cron = "0 0 6 ? * *")
    public void execute() {
        // 判断报告是否已经生成
        checkReportGenerate();

        log.info("*****************开始生成Html币的报告*************");
        wxPushHelper.pushReport(null);
        log.info("*****************结束生成Html币的报告*************");
    }

    // 检测报告是否生成
    private void checkReportGenerate() {
        String currentDate = DateUtil.currentDate2Str("yyyyMMdd");
        String btcReport = "report_" + currentDate + "_1";
        HtmlReportGenerateVO bitCoinReport = bccRedisService.getBitCoinReport(btcReport);
        if (bitCoinReport == null) {
            log.warn("【推送报告时候，检测到报告没有生成，进行重新生成报告】");
            htmlReportGenerateAllTask.execute();
            log.info("【推送报告时候，检测到报告没有生成，生成报告结束】");
        }
    }

}
